/*******************************************************************************
 *
 *    Copyright (c) Baina Info Tech Co. Ltd
 *
 *    First-Aid
 *    
 *    Rating
 *    TODO File description or class description.
 *
 *    @author: zuochen
 *    @since:  2011-10-15
 *    @version: 1.0
 *
 ******************************************************************************/

package com.sdk.markting.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * Rating of First-Aid.
 * 
 * @author zuochen
 * 
 */

public class Rating {
	private Context mContext;
	private static Rating mRating;
	private final SharedPreferences mSharedPreferences;
	private static final String APP_RUNS_TIMES = "app_runs_times";
	private static final int DEFUALT_APP_RUNS_TIMES = 0;
	private static int MAX_APP_RUNS_TIMES = 2;
	private static final String IS_HAS_RATED = "is_has_rated";

	public static Rating getInstance(Context context) {
		if (null == mRating) {
			mRating = new Rating(context);
		}
		return mRating;
	}

	private Rating(Context context) {
		mSharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(context);
	}

	public int getRunTime() {
		return mSharedPreferences
				.getInt(APP_RUNS_TIMES, DEFUALT_APP_RUNS_TIMES);
	}

	public void increaseRunTime() {
		mSharedPreferences.edit().putInt(APP_RUNS_TIMES, getRunTime() + 1)
				.commit();
	}

	public boolean isHasRated() {
		return mSharedPreferences.getBoolean(IS_HAS_RATED, false);
	}

	public void saveIsHasRated() {
		increaseRunTime();
		mSharedPreferences.edit().putBoolean(IS_HAS_RATED, true).commit();
	}

	public boolean isShouldShowRating() {
		if (getRunTime() < MAX_APP_RUNS_TIMES) {
			increaseRunTime();
			return false;
		}
		if (isHasRated()) {
			increaseRunTime();
			return false;
		}
		return true;

	}
}
